package com.honey.sql92script;

import java.util.List;
import java.util.Properties;

import org.w3c.dom.Element;

import com.honey.configureation.holder.ConfigurationHolder;
import com.honey.configureation.xmlparser.ParseElement;
import com.honey.core.utils.StringUtility;
import com.honey.sql92script.parser.Sql92VendorParser;

/**
 * 插件配置
 * @author Administrator
 *
 */
public class Configuration implements ConfigurationHolder {
	
	/** 数据库名称 */
	private Vendor vendor;

	/** 数据库脚本文件路径 */
	private String path;

	private String fileCharacter="UTF-8";
	
	/** 建库脚本 */
	private List<String> script;
	
	/** 是否在沙箱中运行 */
	private boolean runInSandbox;
	
	public void parserElement(Element element, Properties properties) {
		String vendor = ParseElement.getConfiguration(element, "vendor",properties);
		this.vendor = Vendor.getVendor(vendor);
		this.path = ParseElement.getConfiguration(element, "path",properties);
		this.script = ParseElement.getConfigurationList(element, "script", properties);
		String  runSandbox = ParseElement.getConfiguration(element, "runSandbox",properties);
		if("true".equalsIgnoreCase(runSandbox)){
			runInSandbox = true;
		}else{
			runInSandbox = false;
		}
		Element pathElement = ParseElement.getElementsByTagName(element, "path");
		if(pathElement != null ){
			String tmp = ParseElement.getConfiguration(pathElement, "fileCharacter", properties);
			if( StringUtility.stringHasValue(tmp) ){
				this.fileCharacter = tmp ;
			}
		}
		this.vendor.getScriptParser().parserFile(path , fileCharacter);
	}
	
	public Vendor getVendor() {
		return vendor;
	}

	public void setVendor(Vendor vendor) {
		this.vendor = vendor;
	}

	public String getPath() {
		return path;
	}

	public List<String> getScript() {
		return script;
	}

	public String toXmlElement() {

		return null;
	}

	public boolean isRunInSandbox() {
		return runInSandbox;
	}
	
	public void validate() {

	}
	
	enum Vendor {

		/** mysql数据库 */
		MYSQL("MySQL" ){
			ScriptParser getScriptParser(){
				
				return null;
			}
		},

		/** Oracle数据库 */
		ORACLE("Oracle"){
			ScriptParser getScriptParser(){
				
				return null;
			}
		},
		
		/** Postgre数据库 */
		POSTGRE("Postgre"){
			ScriptParser getScriptParser(){
				
				return null;
			}
		},
		
		/** sqllite数据库 */
		SQLLITE("SQLlite"){
			ScriptParser getScriptParser(){
				
				return null;
			}
		},
		
		/** Sql92 标准 */
		SQL92("SQL92"){
			ScriptParser getScriptParser(){
				
				return new Sql92VendorParser();
			}
		},
		;
		private final String name;
		
		private Vendor(String name) {
			this.name = name;
		}

		static Vendor getVendor(String name) {
			Vendor answer = SQL92;
			if ("MYSQL".equalsIgnoreCase(name)) {
				answer = MYSQL;
			} else if ("ORACLE".equalsIgnoreCase(name)) {
				answer = ORACLE;
			} else if ("POSTGRE".equalsIgnoreCase(name)) {
				answer = POSTGRE;
			} else if ("SQLLITE".equalsIgnoreCase(name)) {
				answer = SQLLITE;
			}
			return answer;
		}
		
		abstract ScriptParser getScriptParser();
		
		@Override
		public String toString() {
			return this.name;
		}
	}

}
